package JZ.JZ17;

/**
 * JZ17 树的子结构
 * 输入两棵二叉树A，B，判断B是不是A的子结构。（ps：我们约定空树不是任意一个树的子结构）
 * @author corn
 * @create 2020-07-18-21:40
 */
public class HasSubtreeJZ17 {
    public static void main(String[] args) {
        TreeNode2 tree1 = new TreeNode2(2);
        TreeNode2 tree2 = new TreeNode2(2);
        boolean b = Solution11.isSubStructure(tree1,tree2);
        System.out.println(b);
    }
}
class TreeNode2 {
     int val = 0;
     TreeNode2 left = null;
     TreeNode2 right = null;

     public TreeNode2(int val) {
        this.val = val;
    }
 }
class Solution11 {
        public static boolean isSubStructure(TreeNode2 A, TreeNode2 B) {
            return (A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
        }

        public static boolean recur(TreeNode2 A,TreeNode2 B) {
            if(B == null) return true;
            if(A == null || A.val != B.val) return false;
            return recur(A.left , B.left) && recur(A.right , B.right);
        }
}
